package com.xuzz.study.thread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 2个线程，交替打印奇数 偶数
 */
public class OddEvenThread {

    /**
     * 通过object的wait notify方法来实现
     */
    private static Object monitor = new Object();
    public static void runWithWait()
    {
        //第一个线程，打印奇数
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=1;i<=100;i++)
                {
                    synchronized (monitor){
                        if(i%2==0)
                        {
                            continue;
                        }
                        System.out.println("奇数:"+i);
                        try {
                            monitor.notify();
                            monitor.wait();
                        }
                        catch (InterruptedException e){
                            e.printStackTrace();
                        }
                    }
                }
                /*while(true){
                    synchronized (monitor){
                        if(counter%2==0)
                        {
                            //非奇数，下一次循环
                            continue;
                        }
                        System.out.println("奇数:"+counter);
                        if(++counter>100)
                        {
                            return;
                        }
                        try {
                            monitor.notify();
                            monitor.wait();
                        }
                        catch (InterruptedException e){
                            e.printStackTrace();
                        }
                    }
                }*/
            }
        }
        ).start();

        //第而个线程，打印偶数
        new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=1;i<=100;i++)
                {
                    synchronized (monitor){
                        if(i%2!=0)
                        {
                            continue;
                        }
                        System.out.println("偶数:"+i);
                        try {
                            monitor.notify();
                            monitor.wait();
                        }
                        catch (InterruptedException e){
                            e.printStackTrace();
                        }
                    }
                }
                /*while (true) {
                    synchronized (monitor) {
                        if (counter % 2 != 0) {
                            //非偶数，下一次循环
                            continue;
                        }
                        System.out.println("偶数:" + counter);
                        if(++counter>100)
                        {
                            return;
                        }
                        try {
                            monitor.notify();
                            monitor.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }*/
            }
        }).start();
    }

    /**
     *
     */
    private static final ReentrantLock lock = new ReentrantLock();
    private static final Condition oddCondition=lock.newCondition();
    private static final Condition evenCondition=lock.newCondition();
    private static String flag="odd";  //线程通信标识
    public static void runWithCondition()
    {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("t1 start");
                for(int i=1;i<=100;i++) {
                    if(i%2==0)
                    {
                        continue;
                    }
                    lock.lock();
                    if(flag!="odd")
                    {
                        try {
                            oddCondition.await();
                        }catch (InterruptedException e){}
                    }

                    System.out.println("奇数:"+i);
                    flag="even";
                    evenCondition.signalAll();
                    oddCondition.signalAll();
                    lock.unlock();
                }
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=1;i<=100;i++) {
                    if(i%2!=0)
                    {
                        continue;
                    }
                    lock.lock();
                    if(flag!="even")
                    {
                        try {
                            evenCondition.await();
                        }catch (InterruptedException e){}
                    }

                    System.out.println("偶数:"+i);
                    flag="odd";
                    oddCondition.signalAll();
                    evenCondition.signalAll();
                    lock.unlock();
                }

            }
        });
        t1.start();
        t2.start();
    }
}
